package org.gruposp2p.dnie.ui.panels;

import dniegp2pclient.DNIeGP2PClientApp;
import dniegp2pclient.RuntimeData;
import org.gruposp2p.dnie.model.DNIeSignedDocumentComment;
import org.gruposp2p.dnie.ui.dialogs.AddCommentDialog;
import org.gruposp2p.dnie.utils.DateUtils;

/**
 *
 * @author jj
 */
public class CommentPanel extends javax.swing.JPanel {

    /** Creates new form CommentPanel */
    public CommentPanel() {
        initComponents();
        commentTextArea.setEditable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addCommentButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        commentTextArea = new javax.swing.JTextArea();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Comentario"));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(CommentPanel.class);
        addCommentButton.setIcon(resourceMap.getIcon("addCommentButton.icon")); // NOI18N
        addCommentButton.setText("A\u00F1adir comentario");
        addCommentButton.setName("addCommentButton"); // NOI18N
        addCommentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCommentButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        commentTextArea.setColumns(20);
        commentTextArea.setRows(5);
        commentTextArea.setName("commentTextArea"); // NOI18N
        jScrollPane1.setViewportView(commentTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
                    .addComponent(addCommentButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addCommentButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addCommentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCommentButtonActionPerformed
        AddCommentDialog addCommentDialog = new AddCommentDialog(
                DNIeGP2PClientApp.getApplication().getView().getFrame(), true);
        addCommentDialog.setText(commentTextArea.getText());
        addCommentDialog.setVisible(true);
        String textAdded = addCommentDialog.getText();
        if (textAdded != null && !"".equals(textAdded)) {
            commentTextArea.setText(addCommentDialog.getText());
            addCommentButton.setText("Editar comentario");
        }
    }//GEN-LAST:event_addCommentButtonActionPerformed

    public void setCommentText (String text) {
         commentTextArea.setText(text);
    }

    public void setSigned (boolean isSigned) {
        if (isSigned){
            addCommentButton.setEnabled(false);
        } else {
            addCommentButton.setEnabled(true);
        }
            
    }

    public DNIeSignedDocumentComment getComment () {
        DNIeSignedDocumentComment comment = new DNIeSignedDocumentComment();
        comment.setContent(commentTextArea.getText());
        comment.setDate(DateUtils.getTodayDate());
        comment.setDNIeUserId(RuntimeData.getInstance().user);
        return comment;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCommentButton;
    private javax.swing.JTextArea commentTextArea;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
